package com.smcdd.cdb.dbo.cdb_dbo.service.presenter;

import android.app.Dialog;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.AllInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.AllInfo_vT;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IDateInfos;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DialogUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.StringUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/7/5 22:30
 * 邮箱：zhanghuaiha@gmail.com
 */

public class DateInfosPresenter extends BasePresenter<IDateInfos> {

    private String versionName;
    private String eid;
    private String history_commission,today_commission;
    private String history_ok_num,today_ok_num;
    private Dialog dialog;
    private DecimalFormat de;

    public DateInfosPresenter(BaseActivity context) {
        super(context);
    }

    public void loadDate(){
        versionName = ObtainVersion.getVersionName(mContext);
        eid = App.SP.getString("id", "");
        de = new DecimalFormat("0.00");

    }
    public void getAll_Info(String udid){
        uploadDialog();
        long l = System.currentTimeMillis();
        String time = l + "";
        String date = getTime(new Date(l));
        String encry = ObtainEncry.getAll_info("2", time, versionName, NetWorkCodeInfo.ALL_INFO, udid, eid, date);
        RetrofitFactory.getmInstanse().mApi.getAll_info_v2("2",time,versionName,NetWorkCodeInfo.ALL_INFO,udid,eid,date,encry)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<AllInfo_vT>>() {
                    @Override
                    public void success(BaseResponseEntity<AllInfo_vT> allInfoBaseResponseEntity) {
                        if (allInfoBaseResponseEntity.code == 200) {
                            if (allInfoBaseResponseEntity.data != null) {
                                AllInfo_vT data = allInfoBaseResponseEntity.data;

                                //已铺店铺
//                                if (data.getMerger_store_num() != null) getView().getReach().setText(data.getMerger_store_num());
                                //今日达标
//                                if (data.getMerger_ok_num() != null) getView().getDa().setText(data.getMerger_ok_num());
                                //本月达标店铺
//                                if (data.getMonth_ok_num() != null) getView().getLei().setText(data.getMonth_ok_num());
                                //达标率
//                                String aDouble = div(Double.parseDouble(data.getMerger_ok_num()) *100, Double.parseDouble(data.getMerger_store_num()),2);
//                                getView().getDbl().setText(aDouble+"%");
//                                if (aDouble.equals("0")) {
//                                    getView().getDbl().setText(aDouble+"%");
//                                }else{
//                                    double v = Double.parseDouble(aDouble) * 100;
//                                    getView().getDbl().setText(de.format(v)+"%");
//                                }
                                //我的支付
                                int zong_num = data.getPay_mima_num() + data.getPay_paper_num();
                                Double zong_money = data.getPay_mima_money() + data.getPay_paper_money();
                                getView().getPay_zong_num().setText(zong_num+"");
                                getView().getPay_mima_num().setText(data.getPay_mima_num()+"");
                                getView().getPay_paper_num().setText(data.getPay_paper_num()+"");
                                getView().getPay_zong_money().setText(zong_money+"");
                                getView().getPay_mima_money().setText(data.getPay_mima_money()+"");
                                getView().getPay_paper_money().setText(data.getPay_paper_money()+"");


                                //wifi涨粉
                                if (data.getWifi_sub_count() != null) getView().getWifi_sub_count().setText(data.getWifi_sub_count());
                                //密码涨粉
                                if (data.getMima_sub_count() != null) getView().getMima_sub_count().setText(data.getMima_sub_count());
                                //纸巾机涨粉
                                getView().getPaper_sub_count().setText(data.getPaper_sub_count()+"");
                                //wifi拥有
                                if (data.getAlready_wifi_num() != null) getView().getAlready_wifi_num().setText(data.getAlready_wifi_num());
                                //密码拥有
                                if (data.getAlready_mima_num() != null) getView().getAlready_mima_num().setText(data.getAlready_mima_num());
                                //纸巾机拥有
                                getView().getAlready_paper_num().setText(data.getAlready_paper_num()+"");

                                //wifi今日使用
                                if (data.getUse_wifi_num() != null){
                                    getView().getUse_wifi_num().setText(data.getUse_wifi_num());
                                }
                                //密码今日使用
                                if (data.getUse_mima_num() != null) {
                                    getView().getUse_mima_num().setText(data.getUse_mima_num());
                                }
                                //纸巾机今日使用
                                getView().getUse_paper_num().setText(data.getUse_paper_num()+"");

                                //拥有(总)
                                getView().getLay().setText(Integer.parseInt(data.getAlready_mima_num()) + Integer.parseInt(data.getAlready_wifi_num())+data.getAlready_paper_num()+"");
                                //今日使用(总)
                                getView().getLine().setText(Integer.parseInt(data.getUse_mima_num()) + Integer.parseInt(data.getUse_wifi_num())+data.getUse_paper_num()+"");
                                //涨粉(总)
                                int zong_zhang = Integer.parseInt(data.getMima_sub_count())+ Integer.parseInt(data.getWifi_sub_count())+data.getPaper_sub_count();
                                getView().getSet().setText(zong_zhang+"");

                                // 单台平均 = 涨粉/已铺
                                //wifi单台平均
                                String aDouble1 = getDouble(Double.parseDouble(data.getWifi_sub_count()), Double.parseDouble(data.getUse_wifi_num()));
                                getView().getWifi_count().setText(aDouble1);

                                //密码单台平均
                                String aDouble2 = getDouble(Double.parseDouble(data.getMima_sub_count()), Double.parseDouble(data.getUse_mima_num()));
                                getView().getMima_count().setText(aDouble2);

                                //纸巾机单台平均
                                String aDouble5 = getDouble((double) data.getPaper_sub_count(), (double)data.getUse_paper_num());
                                getView().getPaper_count().setText(aDouble5);


                                //单台平均涨粉(总)
                                double i = Double.parseDouble(data.getMima_sub_count()) + Integer.parseInt(data.getWifi_sub_count())+ data.getPaper_sub_count();
                                int i1 = Integer.parseInt(data.getUse_mima_num()) + Integer.parseInt(data.getUse_wifi_num())+data.getUse_paper_num();
                                String aDouble3 = getDouble(i, (double) i1);
                                getView().getShop().setText(aDouble3);

                                //今日提成
                                //历史已铺店铺达标提成
                                if (data.getHistory_commission() != null) {
                                    history_commission = data.getHistory_commission();
                                }
                                //今日已铺店铺达标提成
                                if (data.getToday_commission() != null) {
                                    today_commission = data.getToday_commission();
                                }
                                //今日达标
                                //历史已铺店铺达标数
                                if (data.getHistory_ok_num() != null) {
                                    history_ok_num = data.getHistory_ok_num();
                                }
                                //今日已铺店铺达标数
                                if (data.getToday_ok_num() != null) {
                                    today_ok_num = data.getToday_ok_num();
                                }
                                //实时在线
                                if (data.getOnline_num()!= null) getView().getsszx().setText(data.getOnline_num());
                                //实时离线
                                if (data.getOffline_num()!= null) getView().getsslx().setText(data.getOffline_num());
                                //平均在线
                                if (data.getAvg_online_num()!= null) getView().getpjzx().setText(data.getAvg_online_num());
                                //时间
                                if (data.getUpdate_time()!= null) getView().getTime().setText("最后更新时间："+data.getUpdate_time());

                                //提成1
//                                String bl = getBl(Double.parseDouble(data.getToday_ok_num()), Double.parseDouble(data.getToday_store_num()));
                                getView().getThree().setText("0.06");
                                String zDouble = getXDouble((double) zong_zhang, 0.06);
                                getView().getOne().setText(zDouble);
                                getView().getTwo().setText(zong_zhang+"");

                                //提成2
//                                String b2 = getBl(Double.parseDouble(data.getHistory_ok_num()), Double.parseDouble(data.getHistory_store_num()));
                                getView().getsix().setText("0.2");
                                String xDouble = getXDouble(zong_money, 0.2);
                                getView().getFour().setText(xDouble);
                                getView().getFive().setText(zong_money+"");

                                //今日提成
                                double c = Double.parseDouble(xDouble) +Double.parseDouble(zDouble);
                                getView().getTc().setText(c+"");
                                //本月累计提成
                                if (data.getMonth_commission() != null) {
                                    double  s = Double.parseDouble(data.getMonth_commission()) + Double.parseDouble(xDouble) + Double.parseDouble(zDouble);
                                    getView().getLjtc().setText(s+"");
                                }

                            }
                        }else {
                            ToastUtil.showToast(allInfoBaseResponseEntity.message);
                        }
                        dialog.dismiss();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        e.printStackTrace();
                        dialog.dismiss();
                        ToastUtil.showToast(mContext.getString(R.string.error_date));
                    }
                });
    }

    private String getDouble(Double d1,Double d2){
        try {
            double v = d1 / d2;
            if (Double.isNaN(v) || Double.isInfinite(v)) {
              return "0";
            }else {
                return de.format(v);
            }
        }catch (Exception e){
            return "0";
        }
    }

    private String getXDouble(Double d1,Double d2){
        try {
            double v = d1 * d2;
            if (Double.isNaN(v) || Double.isInfinite(v)) {
                return "0";
            }else {
                return de.format(v);
            }
        }catch (Exception e){
            return "0";
        }
    }

    public String div(double value1,double value2,int scale){
        //如果精确范围小于0，抛出异常信息

        BigDecimal b1 = new BigDecimal(Double.toString(value1));
        BigDecimal b2 = new BigDecimal(Double.toString(value2));
//        return b1.divide(b2, scale).doubleValue();
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString();
    }


    //倍率
    private String getBl(Double d1,Double d2){
        try {
            double v = d1 / d2;
            if (Double.isNaN(v) || Double.isInfinite(v)) {
                return "1";
            }else {
                if (v <= 0.5) {
                    return "1";
                }else if (v > 0.5 && v <= 0.6) {
                    return "1.2";
                }else if (v > 0.6 && v <= 0.8) {
                    return "1.5";
                }else {
                    return "2";
                }
            }
        }catch (Exception e){
            return "1";
        }
    }

    public String getHC(){
        return history_commission;
    }
    public String getTC(){
        return today_commission;
    }
    public String getHN(){
        return history_ok_num;
    }
    public String getTN(){
        return today_ok_num;
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public void uploadDialog(){
        dialog = DialogUtils.uploadDialog(mContext, mContext.getString(R.string.upload), true);
    }

}
